const request = (params) => {
	/*
	 * 1.初始化值
	 */
	const baseUrl = "http://58.34.36.138:18480"
	const _self = this;
	const url = params.url;
	let method = params.method || 'GET';
	let data = params.data || {};
	/*
	 * 2、添加头部
	 */
	let defaultOpot = {
		'Content-Type': params.contentType ? params.contentType : 'application/json;charset=UTF-8',
		'X-NAS-SDKTOKEN': uni.getStorageSync('nasToken') || '',
		'X-NAS-CLIENTTYPE': '50'
	}
	/*
	 * 3.处理 POST
	 */
	let header = {}
	method = method.toUpperCase()
	// 4、请求地址
	const requestUrl = baseUrl + url;
	// uni.showLoading({
	// 	title: '加载中...'
	// });
	// 6.用 Promise 创建回调
	// console.log(Object.assign({}, defaultOpot, header))
	return new Promise((resolve, reject) => {
		// console.log({
		// 		url: requestUrl,
		// 		method: method,
		// 		header: Object.assign({}, defaultOpot, header),
		// 		data: data,
		// 	}, '请求的内容')
		uni.request({
				url: requestUrl,
				method: method,
				header: Object.assign({}, defaultOpot, header),
				data: data,
			})
			.then(res => { // 成功
				// console.log(res, '返回的内容')
				if (res[1] && res[1].statusCode === 200) {
					let {
						data: dataType
					} = res[1]
					switch (dataType.code * 1) { // 拦截返回参数
						case 200:
							resolve(dataType)
							break;
						default:
							uni.showModal({
								title: '请求数据失败',
								content: dataType.msg,
								confirmText: '确定',
								showCancel: false,
								success: function(res) {
									// if (res.confirm) {} else if (res.cancel) {
									// 	console.log('用户点击取消');
									// }
								}
							})
							resolve('')
							break;
					}
				}
			})
			.catch(err => { // 错误
				reject(err)
			})
			.finally(() => {
				// console.log('不管是否成功都要执行')
				// uni.hideLoading();
			})
	})
}
export default request
